package org.martin.snsrs.crawler.database;

import java.sql.Timestamp;
import java.util.List;
import java.util.Set;

import org.hibernate.FlushMode;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * A data access object (DAO) providing persistence and search support for
 * Activities entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see org.martin.snsrs.crawler.database.Activity
 * @author MyEclipse Persistence Tools
 */

public class ActivitiesDAO extends BaseHibernateDAO {
	private static final Logger log = LoggerFactory
			.getLogger(ActivitiesDAO.class);
	// property constants
	public static final String ACTIVITY_DESC = "activityDesc";
	public static final String ACTIVITY_TYPE = "activityType";
	public static final String ACTIVITY_PIC = "activityPic";
	public static final String ACTIVITY_NAME = "activityName";
	public static final String ACTIVITY_CREATE_USER_ID = "activityCreateUserId";
	public static final String ACTIVITY_LOCATION = "activityLocation";
	
	private static ActivitiesDAO dao;
	
	public static ActivitiesDAO getInstance(){
		if(dao == null)
			dao = new ActivitiesDAO();
		return dao;
	}

	public void save(Activity transientInstance) {
		log.debug("saving Activities instance");
		try {
			Transaction transaction = getSession().beginTransaction();
			getSession().save(transientInstance);
			transaction.commit();
			getSession().flush();
			getSession().close();
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Activity persistentInstance) {
		log.debug("deleting Activities instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Activity findById(java.lang.Integer id) {
		log.debug("getting Activities instance with id: " + id);
		try {
			Activity instance = (Activity) getSession().get(
					"org.martin.snsrs.database.Activities", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Activity instance) {
		log.debug("finding Activities instance by example");
		try {
			List results = getSession()
					.createCriteria("org.martin.snsrs.database.Activities")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Activities instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Activity as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByActivityDesc(Object activityDesc) {
		return findByProperty(ACTIVITY_DESC, activityDesc);
	}

	public List findByActivityType(Object activityType) {
		return findByProperty(ACTIVITY_TYPE, activityType);
	}

	public List findByActivityPic(Object activityPic) {
		return findByProperty(ACTIVITY_PIC, activityPic);
	}

	public List findByActivityName(Object activityName) {
		return findByProperty(ACTIVITY_NAME, activityName);
	}

	public List findByActivityCreateUserId(Object activityCreateUserId) {
		return findByProperty(ACTIVITY_CREATE_USER_ID, activityCreateUserId);
	}

	public List findByActivityLocation(Object activityLocation) {
		return findByProperty(ACTIVITY_LOCATION, activityLocation);
	}

	public List findAll() {
		log.debug("finding all Activities instances");
		try {
			String queryString = "from Activities";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Activity merge(Activity detachedInstance) {
		log.debug("merging Activities instance");
		try {
			Activity result = (Activity) getSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Activity instance) {
		log.debug("attaching dirty Activities instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Activity instance) {
		log.debug("attaching clean Activities instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}